package com.ruoyi.business.service.impl;

import com.ruoyi.business.domain.BusinessComponeParametersVersion;
import com.ruoyi.business.domain.BusinessComponent;
import com.ruoyi.business.mapper.BusinessComponeParametersVersionMapper;
import com.ruoyi.business.mapper.BusinessComponentMapper;
import com.ruoyi.business.service.IBusinessComponeParametersVersionService;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.PinyinUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.UniqueIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 业务组件参数版本表Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-02
 */
@Service
public class BusinessComponeParametersVersionServiceImpl implements IBusinessComponeParametersVersionService
{
    @Autowired
    private BusinessComponeParametersVersionMapper businessComponeParametersVersionMapper;
    @Autowired
    private BusinessComponentMapper businessComponentMapper;
    /**
     * 查询业务组件参数版本表
     *
     * @param id 业务组件参数版本表主键
     * @return 业务组件参数版本表
     */
    @Override
    public BusinessComponeParametersVersion selectBusinessComponeParametersVersionById(Long id)
    {
        return businessComponeParametersVersionMapper.selectBusinessComponeParametersVersionById(id);
    }

    /**
     * 查询业务组件参数版本表列表
     *
     * @param businessComponeParametersVersion 业务组件参数版本表
     * @return 业务组件参数版本表
     */
    @Override
    public List<BusinessComponeParametersVersion> selectBusinessComponeParametersVersionList(BusinessComponeParametersVersion businessComponeParametersVersion)
    {
        return businessComponeParametersVersionMapper.selectBusinessComponeParametersVersionList(businessComponeParametersVersion);
    }

    /**
     * 新增业务组件参数版本表
     *
     * @param businessComponeParametersVersion 业务组件参数版本表
     * @return 结果
     */
    @Override
    public int insertBusinessComponeParametersVersion(BusinessComponeParametersVersion businessComponeParametersVersion)
    {
        //设置当前版本标志及版本号
        BusinessComponent searchBusinessComponent = new BusinessComponent();
        searchBusinessComponent.setEnterpriseCode(businessComponeParametersVersion.getEnterpriseCode());
        searchBusinessComponent.setComponentCode(businessComponeParametersVersion.getComponentCode());
        List<BusinessComponent> businessComponentVos = businessComponentMapper.selectBusinessComponentList(searchBusinessComponent);
        if(businessComponentVos!=null && businessComponentVos.size()>0){
            BusinessComponent businessComponent = businessComponentVos.get(0);
            businessComponeParametersVersion.setIsCurrent(businessComponent.getIsCurrent());
            businessComponeParametersVersion.setVersion(businessComponent.getVersion());
        }
        //校验参数
        verifyInsert(businessComponeParametersVersion);
        //根据参数描述生成参数名称
        if(StringUtils.isEmpty(businessComponeParametersVersion.getParameterName())){
            int index = 1;
            BusinessComponeParametersVersion search = new BusinessComponeParametersVersion();
            search.setEnterpriseCode(businessComponeParametersVersion.getEnterpriseCode());
            search.setComponentCode(businessComponeParametersVersion.getComponentCode());
            businessComponeParametersVersion.setParameterName(PinyinUtils.getPinyin(businessComponeParametersVersion.getParameterDesc()));
            while(true) {
                search.setParameterName(businessComponeParametersVersion.getParameterName());
                //判断参数名称是否已经存在
                if(businessComponeParametersVersionMapper.selectBusinessComponeParametersVersionList(businessComponeParametersVersion).size()>0){
                    businessComponeParametersVersion.setParameterName(PinyinUtils.getPinyin(businessComponeParametersVersion.getParameterDesc())+index);
                    index++;
                }else{
                    break;
                }
            }
        }
        //自动产生参数编码，并保证唯一
        String code;
        BusinessComponeParametersVersion searchBusinessComponent1= new BusinessComponeParametersVersion();
        while (true) {
            code = UniqueIdGenerator.getUniqueClassName(10);
            searchBusinessComponent1.setParameterCode( code);
            List<BusinessComponeParametersVersion> list = businessComponeParametersVersionMapper.selectBusinessComponeParametersVersionList(searchBusinessComponent1);
            if (list.size() == 0) {
                businessComponeParametersVersion.setParameterCode(code);
                break;
            }
        }
        return businessComponeParametersVersionMapper.insertBusinessComponeParametersVersion(businessComponeParametersVersion);
    }

    /**
     * 校验参数
     * @param businessComponeParametersVersion
     */
    private void verifyInsert(BusinessComponeParametersVersion businessComponeParametersVersion) {
        if(businessComponeParametersVersion==null)
            throw new ServiceException("业务组件参数版本不能为空!");
        if(StringUtils.isEmpty(businessComponeParametersVersion.getEnterpriseCode()))
            throw new ServiceException("业务组件参数版本表企业编码不能为空");
        if(StringUtils.isEmpty(businessComponeParametersVersion.getComponentCode()))
            throw new ServiceException("业务组件参数版本表组件编码不能为空");
        if(StringUtils.isEmpty(businessComponeParametersVersion.getParameterDesc()))
            throw new ServiceException("业务组件参数版本表参数描述不能为空");
        if(StringUtils.isEmpty(businessComponeParametersVersion.getParameterType()))
            throw new ServiceException("业务组件参数版本表参数类型不能为空");
        //版本号不能为空
        if(businessComponeParametersVersion.getVersion()==null)
            throw new ServiceException("业务组件参数版本表版本号不能为空");

        //检查相同的参数描述是否已经存在，通一个业务组件下不能重复
        BusinessComponeParametersVersion search = new BusinessComponeParametersVersion();
        search.setEnterpriseCode(businessComponeParametersVersion.getEnterpriseCode());
        search.setComponentCode(businessComponeParametersVersion.getComponentCode());
        search.setParameterDesc(businessComponeParametersVersion.getParameterDesc());
        search.setVersion(businessComponeParametersVersion.getVersion());
        List<BusinessComponeParametersVersion> list = businessComponeParametersVersionMapper.selectBusinessComponeParametersVersionList(search);
        if(list!=null && list.size()>0)
            throw new ServiceException("业务组件参数版本表当前版本下参数描述不能重复");
    }

    /**
     * 修改业务组件参数版本表
     *
     * @param businessComponeParametersVersion 业务组件参数版本表
     * @return 结果
     */
    @Override
    public int updateBusinessComponeParametersVersion(BusinessComponeParametersVersion businessComponeParametersVersion)
    {
        //设置当前版本标志及版本号
        BusinessComponent searchBusinessComponent = new BusinessComponent();
        searchBusinessComponent.setEnterpriseCode(businessComponeParametersVersion.getEnterpriseCode());
        searchBusinessComponent.setComponentCode(businessComponeParametersVersion.getComponentCode());
        List<BusinessComponent> businessComponentVos = businessComponentMapper.selectBusinessComponentList(searchBusinessComponent);
        if(businessComponentVos!=null && businessComponentVos.size()>0){
            BusinessComponent businessComponent = businessComponentVos.get(0);
            businessComponeParametersVersion.setIsCurrent(businessComponent.getIsCurrent());
            businessComponeParametersVersion.setVersion(businessComponent.getVersion());
        }
        if(StringUtils.isEmpty(businessComponeParametersVersion.getParameterCode())) {
            //自动产生参数编码，并保证唯一
            String code;
            BusinessComponeParametersVersion searchBusinessComponent1 = new BusinessComponeParametersVersion();
            while (true) {
                code = UniqueIdGenerator.getUniqueClassName(10);
                searchBusinessComponent1.setParameterCode(code);
                List<BusinessComponeParametersVersion> list = businessComponeParametersVersionMapper.selectBusinessComponeParametersVersionList(searchBusinessComponent1);
                if (list.size() == 0) {
                    businessComponeParametersVersion.setParameterCode(code);
                    break;
                }
            }
        }
        //校验更新数据
        verifyUpdate(businessComponeParametersVersion);
        return businessComponeParametersVersionMapper.updateBusinessComponeParametersVersion(businessComponeParametersVersion);
    }

    /**
     * 校验更新
     * @param businessComponeParametersVersion
     */
    private void verifyUpdate(BusinessComponeParametersVersion businessComponeParametersVersion) {
        if(businessComponeParametersVersion==null)
            throw new ServiceException("业务组件参数版本不能为空!");
        if(businessComponeParametersVersion.getParameterId()==null)
            throw new ServiceException("业务组件参数版本表ID不能为空");
        if(StringUtils.isEmpty(businessComponeParametersVersion.getEnterpriseCode()))
            throw new ServiceException("业务组件参数版本表企业编码不能为空");
        if(StringUtils.isEmpty(businessComponeParametersVersion.getComponentCode()))
            throw new ServiceException("业务组件参数版本表组件编码不能为空");
        if(StringUtils.isEmpty(businessComponeParametersVersion.getParameterDesc()))
            throw new ServiceException("业务组件参数版本表参数描述不能为空");
        if(StringUtils.isEmpty(businessComponeParametersVersion.getParameterType()))
            throw new ServiceException("业务组件参数版本表参数类型不能为空");
        //版本号不能为空
        if(businessComponeParametersVersion.getVersion()==null)
            throw new ServiceException("业务组件参数版本表版本号不能为空");

        //检查相同的参数描述是否已经存在(排查当前id)，通一个业务组件下不能重复
        BusinessComponeParametersVersion search = new BusinessComponeParametersVersion();
        search.setEnterpriseCode(businessComponeParametersVersion.getEnterpriseCode());
        search.setComponentCode(businessComponeParametersVersion.getComponentCode());
        search.setParameterDesc(businessComponeParametersVersion.getParameterDesc());
        search.setVersion(businessComponeParametersVersion.getVersion());
        List<BusinessComponeParametersVersion> list = businessComponeParametersVersionMapper.selectBusinessComponeParametersVersionList(search);
        if(list!=null && list.size()>0){
            for(BusinessComponeParametersVersion businessComponeParametersVersion1:list){
                if(!businessComponeParametersVersion1.getParameterId().equals(businessComponeParametersVersion.getParameterId())){
                    throw new ServiceException("业务组件参数版本表当前版本下参数描述不能重复");
                }
            }
        }


    }

    /**
     * 批量删除业务组件参数版本表
     *
     * @param ids 需要删除的业务组件参数版本表主键
     * @return 结果
     */
    @Override
    public int deleteBusinessComponeParametersVersionByIds(Long[] ids)
    {
        return businessComponeParametersVersionMapper.deleteBusinessComponeParametersVersionByIds(ids);
    }

    /**
     * 删除业务组件参数版本表信息
     *
     * @param id 业务组件参数版本表主键
     * @return 结果
     */
    @Override
    public int deleteBusinessComponeParametersVersionById(Long id)
    {
        return businessComponeParametersVersionMapper.deleteBusinessComponeParametersVersionById(id);
    }

    /**
     * 更新业务组件参数版本表为非当前版本
     *
     * @param componentCode
     * @param enterpriseCode
     */
    @Override
    public int updateCurrentVersionToIsNot(String componentCode, String enterpriseCode) {
        return businessComponeParametersVersionMapper.updateCurrentVersionToIsNot(componentCode, enterpriseCode);
    }

    /**
     * 删除业务组件参数版本表
     *
     * @param componentCode
     * @param enterpriseCode
     * @param version
     */
    @Override
    public int deleteParametersVersionByComponentCode(String componentCode, String enterpriseCode, Integer version) {
        return businessComponeParametersVersionMapper.deleteParametersVersionByComponentCode(componentCode, enterpriseCode, version);
    }
}
